package com.hongxun.budget.ui.activity.ys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.adapter.SpinnerOffBgAdapter;
import com.hongxun.budget.ui.dialog.PointYsChoiseDialog;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.DrawYsMapUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.L;
import com.hongxun.budget.utils.LocationUtils;
import com.hongxun.budget.utils.MyAMapUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.greenentity.ys.EPointEntity;
import com.hongxun.interfaces.ResponseListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CheckActivity extends BaseActivity {


    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.right_save)
    Button rightSave;
    @BindView(R.id.show_span)
    ImageButton showSpan;
    @BindView(R.id.show_code)
    ImageButton showCode;
    @BindView(R.id.map)
    MapView mMapView;
    @BindView(R.id.center_title)
    TextView centerTitle;
    AMap aMap;
    String guid;
    LatLng locationLatlng;//定位位置经纬度
    LocationUtils locationUtils;
    boolean isVisibleSpan;//是否显示档距
    boolean isVisibleBianhao;//是否显示编号
    DrawYsMapUtils drawMapUtils;
    @BindView(R.id.spinner)
    Spinner spinner;
    EPointEntity lastPoint = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_check;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        guid = (String) SPUtils.get(context, "project_ys_guid", "");
        centerTitle.setText(GreenDaoManager.getInstance().getmDaoSession().getEPBaseEntityDao().load(guid).getPBName());
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setText("明细");
        rightSave.setVisibility(View.VISIBLE);
        rightSave.setText("验收");
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        init();
        locationUtils = new LocationUtils(context, getApplicationContext(), 3, 1, this);
        drawMapUtils = new DrawYsMapUtils(this, aMap, isVisibleSpan, isVisibleBianhao, "查看");
        drawMapUtils.setDragIcon(true);
        final String[] deviceArr=getResources().getStringArray(R.array.process_mode);
        List<String> strings = new ArrayList<>();
        strings.add("全部");
        strings.addAll(Arrays.asList(deviceArr));
        spinner.setAdapter(new SpinnerOffBgAdapter<>(context, strings, spinner).setListener(new ResponseListener() {
            @Override
            public void response(Object... data) {
                String work = data[1].toString();
                drawMapUtils.setWork(work);
                if (aMap != null)
                    drawMapUtils.draw();
            }
        }));
        lastPoint = DBUtils.getLastYsPoint(context);
        if (lastPoint != null) {
            aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastPoint.getPPInputlatitude(), lastPoint.getPPInputlongitude()), MyAMapUtils.locationZoom));
        }
    }

    /**
     * 初始化
     */
    private void init() {
        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                SPUtils.put(context, "drag_lat", cameraPosition.target.latitude);//记录拖拽的位置
                SPUtils.put(context, "drag_lgt", cameraPosition.target.longitude);//记录拖拽的位置
                L.d("drag_position", cameraPosition.target.toString());
            }
        });

        aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                drawMapUtils.addMarkerInScreenCenter();
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        locationUtils.stopLocation();
        locationUtils.destroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
        drawMapUtils.draw();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }


    @OnClick({R.id.right_btn, R.id.right_save, R.id.show_code, R.id.show_span, R.id.show_location})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.right_btn://明细
                startActivity(new Intent(context, YsProjectDetailActivity.class));
                break;
            case R.id.right_save://踏勘
                PointYsChoiseDialog dialog = new PointYsChoiseDialog();
                dialog.setTitle("请选择操作和点位").show(getFragmentManager(), "");
                break;
            case R.id.show_code:
                if (isVisibleBianhao) {
                    showCode.setImageResource(R.mipmap.p_show_code_n);
                } else {
                    showCode.setImageResource(R.mipmap.p_show_code_y);
                }
                isVisibleBianhao = !isVisibleBianhao;
                drawMapUtils.setVisibleBianhao(isVisibleBianhao);
                drawMapUtils.draw();
                break;
            case R.id.show_span:
                if (isVisibleSpan) {
                    showSpan.setImageResource(R.mipmap.p_show_span_n);
                } else {
                    showSpan.setImageResource(R.mipmap.p_show_span_y);
                }
                isVisibleSpan = !isVisibleSpan;
                drawMapUtils.setVisibleSpan(isVisibleSpan);
                drawMapUtils.draw();
                break;
            case R.id.show_location:
                if (locationLatlng != null) {//移动到定位位置
                    aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(locationLatlng, MyAMapUtils.locationZoom));
                }
                break;
        }
    }

    @Override
    public void success(int i, JSONObject obj) {
        switch (i) {
            case 999:
                double latitude = obj.getDouble("latitude");
                double longitude = obj.getDouble("longitude");
                if (locationLatlng == null && lastPoint == null) {
                    aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), MyAMapUtils.locationZoom));
                }
                locationLatlng = new LatLng(latitude, longitude);
                SPUtils.put(context, "location_lat", latitude);//记录定位的位置
                SPUtils.put(context, "location_lgt", longitude);//记录定位的位置
                L.d("location_position", locationLatlng.toString());
                break;
        }
    }
    public AMap getaMap() {
        return aMap;
    }
}
